package jp.com.nobnak.commentviewer.cookie;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.impl.cookie.BasicClientCookie2;

public class CookieUtil {
	static private final String SEPARATE = ";";
	static private final String CONNECTOR = "=";
	
	
	static public CookieDict fromString(String cookie) {
		CookieDict dict = new CookieDict();

		String[] pairs = cookie.split(SEPARATE);
		for (String pair : pairs) {
			pair = pair.trim();
			String[] tuple = pair.split(CONNECTOR);
			if (tuple.length != 2)
				continue;
			dict.put(tuple[0], tuple[1]);
		}
		
		return dict;
	}
	static public List<Cookie> listCookies(CookieDict dict, String domain, String path) {
		List<Cookie> result = new ArrayList<Cookie>();
		for (String key : dict.keys())
			for (String value : dict.all(key)) {
				BasicClientCookie cookie = new BasicClientCookie2(key, value);
				cookie.setDomain(domain);
				cookie.setPath(path);
				result.add(cookie);
			}
		return result;
	}
}
